﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tibia;
using Tibia.Objects;
using Tibia.Util;


namespace WindowsFormsApplication1
{
    public partial class Main : Form
    {
        public int index = 0;
        public Client client;
        public Tibia.Objects.Console console;
        public List<Website.CharOnline> onlinePeople;
        public Boolean isActive;
        public int runningFor = 0;

        public Main()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            send(sender, e);
        }

        private void send(object sender, EventArgs e)
        {
            if (index == listBox1.Items.Count)
            {
                index = 0;
                textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Completed the massive private messaging.\n";
                listBox1.Items.Clear();
            }

            message.Text = message.Text.Replace("%name%", listBox1.Items[index].ToString());
            console.Say(message.Text, listBox1.Items[index].ToString());
            message.Text = message.Text.Replace(listBox1.Items[index].ToString(), "%name%");

            label6.Text = Convert.ToString(index);
            label7.Text = Convert.ToString(listBox1.Items.Count);

            textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Sent message to: " + listBox1.Items[index].ToString() + "\r\n";
            progressBar1.Value = (index / listBox1.Items.Count) * 100;
            index++;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            client = Tibia.Util.ClientChooser.ShowBox();
            if (client == null /*|| !client.LoggedIn*/)
            {
                MessageBox.Show("You must have at least one client open and logged in to start this program.");
                Application.Exit();
            }
            console = new Tibia.Objects.Console(client);
            textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Tibia client found.\r\n";
        }

        private Boolean isSelected(string searchString)
        {
            for (int i = 0; i < this.listBox2.SelectedItems.Count; i++)
            {
                if (listBox2.SelectedItems[i].ToString() == searchString)
                {
                    return true;
                }
            }
            return false;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (!this.isActive && listBox1.Items.Count > 0)
            {
                timer1.Interval = Int32.Parse(textBox3.Text);
                timer1.Start();
                timer2.Start();
                this.isActive = true;
                button3.Enabled = true;
                button1.Enabled = false;
                tabControl1.SelectedTab = tabPage2;
                tabPage1.Enabled = false;
                button7.Enabled = true;
                textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Started the massive private messaging.\r\n";
            }
            else
            {
                MessageBox.Show("Make sure you have generated an online-player list.");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.isActive)
            {
                timer1.Stop();
                timer2.Stop();
                runningFor = 0;
                this.isActive = false;
                button1.Enabled = true;
                button3.Enabled = false;
                tabPage1.Enabled = true;
                button7.Enabled = false;
                textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Aborted the massive private messaging by user.\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            tabPage1.Enabled = false;

            Website.WhoIsOnline(comboBox1.Text, delegate(List<Website.CharOnline> i)
            {
                listBox1.Invoke(new EventHandler(delegate
                {
                    for (int z = 0; z < i.Count(); z++)
                    {
                        if (comboBox2.Text == "Higher then")
                        {
                            if (i[z].Level > Int32.Parse(textBox1.Text) && isSelected(i[z].Vocation))
                            {
                                listBox1.Items.Add(i[z].Name);
                            }
                        }
                        else if (comboBox2.Text == "Lower then")
                        {
                            if (i[z].Level < Int32.Parse(textBox1.Text) && isSelected(i[z].Vocation))
                            {
                                listBox1.Items.Add(i[z].Name);
                            }
                        }
                        else if (comboBox2.Text == "Equal to")
                        {
                            if (i[z].Level == Int32.Parse(textBox1.Text) && isSelected(i[z].Vocation))
                            {
                                listBox1.Items.Add(i[z].Name);
                            }
                        }
                    }

                    tabPage1.Enabled = true;
                    textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Found " + listBox1.Items.Count + " characters matching your filter out of the " + i.Count() + " online characters.\r\n";
                }));
            });
        }

        private void tibiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.tpforums.org/forum/forumdisplay.php?f=80");
        }

        private void spammerFloppyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.tpforums.org/forum/showthread.php?t=1750");
        }

        private void tibiaProgrammingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.tpforums.org/");
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            runningFor++;
            label8.Text = Convert.ToString(runningFor) + " seconds";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Cleared the character list manually.\r\n";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(textBox4.Text);
            textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Added " + textBox4.Text + " manually to the character list.\r\n";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Removed " + listBox1.SelectedItem.ToString() + " manually from the character list.\r\n";
            listBox1.Items.Remove(listBox1.SelectedItem);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (button7.Text == "Pause")
            {
                timer1.Stop();
                timer2.Stop();
                button7.Text = "Resume";
                textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Paused.\r\n";

            }
            else
            {
                timer1.Start();
                timer2.Start();
                button7.Text = "Pause";
                textBox2.Text = textBox2.Text + "[" + DateTime.Now.ToString() + "] Resumed.\r\n";
            }
        }
    }
}
